package basic.sort.tree;

import java.util.ArrayDeque;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @author distantstar
 * @version 1.0
 * @date 2021
 * @description basic.sort.tree
 * @since JDK1.8
 */
public class LevelErgodic {

    private static class Node {
        public int value;
        public Node left;
        public Node right;

        public Node(int v) {
            value = v;
        }
    }

    private static void sequenceErgodic(Node head) {
        if (head == null) {
            return;
        }

        Queue<Node> queue = new ArrayDeque<>();
        queue.offer(head);

        while (!queue.isEmpty()) {
            Node node = queue.poll();

            if (node.left != null) {
                queue.offer(node.left);
            }
            if (node.right != null) {
                queue.offer(node.right);
            }

            System.out.print(node.value + " ");
        }
    }

    public static void main(String[] args) {
        Node head = new Node(1);
        head.left = new Node(2);
        head.right = new Node(3);
        head.left.left = new Node(4);
        head.left.right = new Node(5);
        head.right.left = new Node(6);
        head.right.right = new Node(7);

        sequenceErgodic(head);
    }
}
